package com.xywh.wanandroid.compose_ui.main.bottomBar

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.runtime.Composable
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import com.xywh.composeLibrary.statex.StateCompose
import com.xywh.composeLibrary.statex.SwipeRefreshList
import com.xywh.wanandroid.compose_ui.main.ArticleItem
import com.xywh.wanandroid.compose_ui.main.cancelOrAddCollectArticle
import com.xywh.wanandroid.data.model.bean.ArticleItem
import com.xywh.wanandroid.viewmodel.MainComposeViewModel

@Composable
fun SquareScreen(
    mainComposeViewModel: MainComposeViewModel,
    innerPadding: PaddingValues,
    navigateWebView: (String, String) -> Unit
) {

//    val squareArticles =
//        mainComposeViewModel.squareArticles.collectAsLazyPagingItems()
    val squareArticles =
        mainComposeViewModel.collectSquareArticlesLiveData.collectAsLazyPagingItems()

    StateCompose(squareArticles) {
        SquareContent(
            innerPadding,
            squareArticles,
            navigateWebView
        ) { id, collect ->
            cancelOrAddCollectArticle(collect, mainComposeViewModel, id)
        }
    }
}

@Composable
@OptIn(ExperimentalMaterialApi::class)
fun SquareContent(
    innerPadding: PaddingValues,
    squareArticles: LazyPagingItems<ArticleItem>,
    navigateWebView: (String, String) -> Unit,
    favoriteOnClick: (Int, Boolean) -> Unit

) {

    SwipeRefreshList(
        collectAsLazyPagingItems = squareArticles
    ) {

        items(
            count = squareArticles.itemCount,
            key = { index ->
                val article = squareArticles[index]
                "${article?.id ?: ""}${index}"
            }) {
            val itemData = squareArticles[it] ?: return@items
            ArticleItem(itemData, false, favoriteOnClick = favoriteOnClick) { title, link ->
                navigateWebView.invoke(title, link)
            }
        }
    }

//    val refresh by remember { mutableStateOf(false) }
//    val pullRefreshState = rememberPullRefreshState(refresh, {
//        squareArticles.refresh()
//    })
//
//
//    Box(
//        modifier = Modifier
//            .fillMaxSize()
//            .pullRefresh(pullRefreshState)
//    ) {
//
//        val listState = rememberLazyListState()
//
//        LazyColumn(
//            state = listState,
//            // consume insets as scaffold doesn't do it by default
//            modifier = Modifier
//                .consumeWindowInsets(innerPadding)
//                .fillMaxHeight(),
////                .padding(bottom = innerPadding.calculateBottomPadding()),
////            contentPadding = innerPadding,
//        ) {
//
//
//            items(
//                count = squareArticles.itemCount,
//                key = { index ->
//                    val article = squareArticles[index]
//                    "${article?.id ?: ""}${index}"
//                }) {
//                val itemData = squareArticles[it] ?: return@items
//                ArticleItem(itemData, false, favoriteOnClick = favoriteOnClick) { title, link ->
//                    navigateWebView.invoke(title, link)
//                }
//            }
//
//
//        }
//
//        Box(
//            Modifier
//                .padding(10.dp)
//                .align(Alignment.BottomEnd)
//        ) {
//            ListFloatingActionButton(rememberCoroutineScope(), listState)
//        }
//        PullRefreshIndicator(
//            refresh,
//            pullRefreshState,
//            Modifier.align(Alignment.TopCenter),
//            scale = true
//        )
//    }
}
